<?php
include_once('baseModel.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/smartrfid/model/rfidInfo.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/smartrfid/model/itemInfo.php');

class Item extends BaseModel {
  public $_table="item";

  public $id;
  public $identifier;
  public $ticketIdentifier;
  public $ticketItemNumber;
  public $ticket_id;

  
  function loadByTicketId($id) {
    $query = "SELECT * FROM " . $this->_table . " WHERE ticket_id = " . $id;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $items = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($items, clone $this);
    }
    return $items;
  }
  
  function loadByLimit($limit) {
    
    $query = "SELECT * FROM `" . $this->_table . "`";
    if($limit) {
      $query .= " limit 0," . $limit;
    }
    //print $query;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $items = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($items, clone $this);
    }
    return $items;
  }
  
  function loadByRfid($rfid) {
    $rfidInfo = new RfidInfo();
    $rfidInfo->loadByRfid($rfid);
    
    $itemInfo = new ItemInfo();
    $itemInfo->load($rfidInfo->itemInfo_id);
    
    $this->load($itemInfo->item_id);
    
    return $this;
  }
  
  function loadByIdentifier($id) {
    $query = "SELECT * FROM " . $this->_table . " WHERE identifier = " . $id;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $items = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($items, clone $this);
    }
    return $items;
  }
  
  function loadRepairItems($limit) {
    $query = "SELECT * FROM `".$this->_table."` `i`";
    $query .= " INNER JOIN itemStatus `is` ";
    $query .= " ON `is`.item_id =  `i`.id ";
    $query .= " WHERE `is`.isStaged=1 AND `is`.isChecked=1 AND `is`.isReturned=1 ";
    $query .= " LIMIT 0,".$limit;
    
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $items = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($items, clone $this);
    }
    return $items;
  }
  
  function loadUntaggedItems($limit) {
    $query = "SELECT `". $this->_table ."`.* FROM `". $this->_table ."` INNER JOIN `itemInfo`"; 
    $query .= "ON (`". $this->_table ."`.`id`=`itemInfo`.`item_id`) ";
    $query .= "LEFT JOIN `rfidInfo` ON (`rfidInfo`.`itemInfo_id`=`itemInfo`.`id`) ";
    $query .= "WHERE `rfidInfo`.`rfidNumber` IS NULL";
    if($limit) {
      $query .= " LIMIT 0,".$limit;
    }
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $items = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($items, clone $this);
    }
    return $items;
  }
  
  
}
